# Replication archive for 
# Coppock, Alexander, Donald P. Green, and Ethan Porter. 2022. 
# Does Digital Advertising Affect Vote Choice? Evidence from a Randomized Field Experiment 
# Research & Politics. 

rm(list = ls())
# setwd() to replication archive

library(tidyverse)
library(estimatr)
library(texreg)

precinct_level <- read_rds("CGP_2022_precinct_level.rds")

# vote share --------------------------------------------------------------

fit_1 <-
  lm_robust(
    dem_two_party_share_2018 ~ Z + cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_2 <-
  lm_robust(
    dem_two_party_share_2018 ~ Z +
      dem_two_party_share_2016_nona + vote_2016_missing +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing +
      cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_3 <-
  lm_robust(
    dem_two_party_share_2018 ~ treatment + cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_4 <-
  lm_robust(
    dem_two_party_share_2018 ~ treatment +
      dem_two_party_share_2016_nona + vote_2016_missing +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing +
      cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

# to check coefficient names
texreg(list(fit_1, fit_2, fit_3, fit_4),
       include.ci = FALSE,
       digits = 3)

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.map = list(
    "Z" = "Any Treatment Video",
    "treatmentvideo_1" = "Treatment Video 1",
    "treatmentvideo_2" = "Treatment Video 2",
    "dem_two_party_share_2016_nona" = "Two Party Vote Share (2016)",
    "vote_2016_missingTRUE" = "Missingness Indicator (2016)",
    "dem_two_party_share_2014_nona"  = "Two Party Vote Share (2014)",
    "vote_2014_missingTRUE" = "Missingness Indicator (2014)",
    "dem_two_party_share_2012_nona" = "Two Party Vote Share (2012)",
    "vote_2012_missingTRUE" = "Missingness Indicator (2012)",
    "(Intercept)" =  "Intercept"
  ),
  digits = 4,
  stars = 0.05,
  custom.note = "%stars. CR2 SEs are in parentheses. All models include fixed effects for congressional district.",
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_share_CD.tex"
)

# vote margin ---------------------------------------------------

fit_1 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ Z + cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_2 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ Z + 
      dem_two_party_vote_margin_2016_nona + vote_2016_missing +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing +
      cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_3 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ treatment + cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_4 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ treatment + 
      dem_two_party_vote_margin_2016_nona + vote_2016_missing +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing +
      cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.map = list(
    "Z" = "Any Treatment Video",
    "treatmentvideo_1" = "Treatment Video 1",
    "treatmentvideo_2" = "Treatment Video 2",
    "dem_two_party_vote_margin_2016_nona" = "Two Party Vote Margin (2016)",
    "vote_2016_missingTRUE" = "Missingness Indicator (2016)",
    "dem_two_party_vote_margin_2014_nona"  = "Two Party Vote Margin (2014)",
    "vote_2014_missingTRUE" = "Missingness Indicator (2014)",
    "dem_two_party_vote_margin_2012_nona" = "Two Party Vote Margin (2012)",
    "vote_2012_missingTRUE" = "Missingness Indicator (2012)",
    "(Intercept)" =  "Intercept"
  ),
  digits = 2,
  stars = 0.05,
  custom.note = "%stars. CR2 SEs are in parentheses. All models include fixed effects for congressional district.",
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_margin_CD.tex"
)

# vote total ---------------------------------------------------

fit_1 <-
  lm_robust(
    vote_total_2018 ~ Z + cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_2 <-
  lm_robust(
    vote_total_2018 ~ Z + 
      vote_total_2016_nona + vote_2016_missing +
      vote_total_2014_nona + vote_2014_missing +
      vote_total_2012_nona + vote_2012_missing +
      cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_3 <-
  lm_robust(
    vote_total_2018 ~ treatment + cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_4 <-
  lm_robust(
    vote_total_2018 ~ treatment + 
      vote_total_2016_nona + vote_2016_missing +
      vote_total_2014_nona + vote_2014_missing +
      vote_total_2012_nona + vote_2012_missing +
      cd_jumble,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.map = list(
    "Z" = "Any Treatment Video",
    "treatmentvideo_1" = "Treatment Video 1",
    "treatmentvideo_2" = "Treatment Video 2",
    "vote_total_2016_nona" = "Turnout (2016)",
    "vote_2016_missingTRUE" = "Missingness Indicator (2016)",
    "vote_total_2014_nona"  = "Turnout (2014)",
    "vote_2014_missingTRUE" = "Missingness Indicator (2014)",
    "vote_total_2012_nona" = "Turnout (2012)",
    "vote_2012_missingTRUE" = "Missingness Indicator (2012)",
    "(Intercept)" =  "Intercept"
  ),
  digits = 2,
  stars = 0.05,
  custom.note = "%stars. CR2 SEs are in parentheses. All models include fixed effects for congressional district.",
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_total_CD.tex"
)

